define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
            index: function () {

                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        index_url: 'dormitory/student/index',
                        add_url: 'dormitory/student/add',
                        edit_url: 'dormitory/student/edit',
                        del_url: 'dormitory/student/del',
                        multi_url: '',
                    }
                });

                var table = $("#table");

                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    sortName: 'student.id',
                    columns: [
                        [
                            {field: 'number', title: __('学号'), operate: false, formatter: Controller.api.formatter.number},
                            {field: 'name', title: __('姓名'), operate: false},
                            {field: 'gender_text', title: __('性别'), operate: false},
                            {field: 'school_name', title: __('学校'), operate: false},
                            {field: 'grade_name', title: __('年级'), operate: false},
                            {field: 'class_name', title: __('班级'), operate: false},
                            {field: 'group_name', title: __('角色组'), operate: false},
                            // {field: 'garden_name', title: __('园区'), operate: false},
                            // {field: 'building_name', title: __('楼栋'), operate: false},
                            {field: 'dorm_name', title: __('寝室'), operate: false},
                            {field: 'bed_number', title: __('床位'), operate: false},
                            {field: 'student.number', title: __('学号'), operate: 'LIKE %...%', visible: false},
                            {field: 'student.name', title: __('姓名'), operate: 'LIKE %...%', visible: false},
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: table,
                                events: Table.api.events.operate,
                                formatter: Table.api.formatter.operate
                            }
                        ]
                    ],
                    //禁用默认搜索
                    search: true,
                    //启用普通表单搜索
                    commonSearch: true,
                    //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                    searchFormVisible: true,
                    //分页大小
                    pageSize: 12

                });

                // 为表格绑定事件
                Table.api.bindevent(table);

                $(document).on('click', '.btn-import', function () {
                    Backend.api.open('dormitory/student/import', '导入');
                });

                $(document).on('click', '.number', function () {
                    Backend.api.open('dormitory/student/detail?ids=' + $(this).data('id'), '详细信息');
                });


            },
            add: function () {
                //Form.api.bindevent($("form[role=form]"));
                Controller.api.bindevent();
            },
            edit: function () {
                $(document).on('change', '.bed_id', function () {
                    var student_id = $('#student_id').val();
                    var bed_id = $(this).val();
                    $.ajax({
                        url: "dormitory/student/check",
                        type: 'post',
                        dataType: 'json',
                        data: {student_id: student_id, bed_id: bed_id},
                        success: function (ret) {
                            if (ret.code === 1) {
                                if (ret.msg) {
                                    Layer.alert(ret.msg, {icon: 6});
                                }
                            } else {
                                Layer.alert(ret.msg, {icon: 5});
                            }
                        }, error: function (e) {
                            Backend.api.toastr.error(e.message);
                        }
                    });

                });

                Controller.api.bindevent();
            },
            detail: function() {
                Controller.api.bindevent();
            },
            ranking: function () {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        ranking_url: 'dormitory/student/ranking',
                    }
                });

                var table = $("#table");

                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.ranking_url,
                    sortName: 'score',
                    //禁用默认搜索
                    search: false,
                    //启用普通表单搜索
                    commonSearch: false,
                    //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                    searchFormVisible: false,
                    //分页大小
                    pageSize: 12,
                    columns: [
                        [
                            {field: 'num', title: __('序号'), operate: false},
                            {field: 'sort', title: __('排名'), operate: false},
                            {field: 'number', title: __('学号'), operate: false},
                            {field: 'name', title: __('姓名'), operate: false},
                            {field: 'gender_text', title: __('性别'), operate: false},
                            {field: 'score', title: __('当前积分'), operate: false},
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: table,
                                events: Table.api.events.operate,
                                buttons: [
                                    {
                                        name: 'record',
                                        title: '积分明细',
                                        text: '积分明细',
                                        icon: 'fa',
                                        classname: 'btn btn-success btn-xs btn-click',
                                        click: function (e, data) {
                                            Backend.api.open('dormitory/student/record?student_id=' + data.id, __('积分明细--') + data.name);
                                        }
                                    },
                                ],
                                formatter: Table.api.formatter.operate
                            }

                        ]
                    ],


                });

                // 为表格绑定事件
                Table.api.bindevent(table);
            },
            import: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            record: function () {
                // 初始化表格参数配置
                Table.api.init({
                    extend: {
                        record_url: 'dormitory/student/record',
                        del_url: 'dormitory/student/delRecord',
                    }
                });

                var table = $("#table");

                // 初始化表格
                table.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.record_url + '?student_id=' + Config.student_id,
                    sortName: 'id',
                    columns: [
                        [
                            {field: 'state', checkbox: true, },
                            {field: 'score', title: __('点评分数'), operate: false},
                            {field: 'behavior_name', title: __('点评行为'), operate: false},
                            {
                                field: 'created_at',
                                title: __('点评时间'),
                                sortable: true,
                                formatter: Table.api.formatter.datetime,
                                operate: 'RANGE',
                                addclass: 'datetimerange'
                            },
                            {field: 'record_user_group', title: __('点评人'), operate: false},
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: table,
                                events: Table.api.events.operate,
                                formatter: Table.api.formatter.operate
                            }

                        ]
                    ],
                    //禁用默认搜索
                    search: true,
                    //启用普通表单搜索
                    commonSearch: false,
                    //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                    searchFormVisible: true,
                    //分页大小
                    pageSize: 12

                });

                // 为表格绑定事件
                Table.api.bindevent(table);

                //当内容渲染完成后
                table.on('post-body.bs.table', function (e, settings, json, xhr) {

                    //点击切换/排序/删除操作后刷新左侧菜单
                    $(".btn-change[data-id],.btn-delone,.btn-dragsort").data("success", function (data, ret) {
                        Controller.api.refreshtable();
                        return false;
                    });

                });
                //批量删除后的回调
                $(".toolbar > .btn-del,.toolbar .btn-more~ul>li>a").data("success", function (e) {
                    Controller.api.refreshtable();
                });

            },
            api: {
                bindevent: function () {
                    $('.group_type').on('change', function () {
                        if ($(this).val() === '2') {
                            var timestamp = Date.parse(new Date()) + 7 * 86400000;
                            var date = Controller.api.getMyDate(timestamp);
                            $('input[name=group_end_time]').val(date);
                            $('#group_end_time').show();
                        } else {
                            $('#group_end_time').hide();
                        }
                    });

                    $('.school').on('change', function () {
                        $.ajax({
                            url: "common/gardens",
                            type: 'post',
                            dataType: 'json',
                            data: {school_id: $(this).val()},
                            success: function (ret) {
                                if (ret.code === 1) {
                                    $('.garden').html(Template('tpl', ret));
                                } else {
                                    Backend.api.toastr.error(ret.msg);
                                }
                            }, error: function (e) {
                                Backend.api.toastr.error(e.message);
                            }
                        });
                    });

                    Form.api.bindevent($("form[role=form]"));
                    Form.events.datetimepicker($("form[role=form]"));
                },
                getMyDate: function (str) {
                    var oDate = new Date(str),
                        oYear = oDate.getFullYear(),
                        oMonth = oDate.getMonth() + 1,
                        oDay = oDate.getDate(),
                        oHour = oDate.getHours(),
                        oMin = oDate.getMinutes(),
                        oSen = oDate.getSeconds(),
                        oTime = oYear + '-' + Controller.api.getzf(oMonth) + '-' + Controller.api.getzf(oDay) + ' ' + Controller.api.getzf(oHour) + ':' +
                            Controller.api.getzf(oMin) + ':' + Controller.api.getzf(oSen);//最后拼接时间
                    return oTime;
                },
                getzf: function (num) {
                    if (parseInt(num) < 10) {
                        num = '0' + num;
                    }
                    return num;
                },
                refreshtable: function () {
                    $('#table').bootstrapTable('refresh');
                    window.parent.$(".toolbar .btn-refresh").trigger('click');
                },
                formatter: {
                    number: function (value, row, index) {
                        return '<a class="number" data-id="'+row.id+'" href="javascript:void(0)" target="_blank">' + value + '</a>';
                    }
                }
            }
    };
    return Controller;
});